//
//  MainTabBarViewController.m
//  卢卡的时光网
//
//  Created by 卡神 on 15/7/17.
//  Copyright © 2015年 lok. All rights reserved.
//

#import "MainTabBarViewController.h"
#import "MovieViewController.h"
#import "NewsViewController.h"
#import "TopViewController.h"
#import "CinemaViewController.h"
#import "MoreViewController.h"
#import "LKButton.h"
#import "BaseNavController.h"
#import "PhotoViewController.h"

@interface MainTabBarViewController () <UINavigationControllerDelegate>
{
    NSMutableArray *nvcArray;
    UIImageView *_selectedView;
}
@end

@implementation MainTabBarViewController


- (void)navigationController:(nonnull UINavigationController *)navigationController willShowViewController:(nonnull UIViewController *)viewController animated:(BOOL)animated {
    self.tabBar.hidden = [navigationController.viewControllers count] > 1? YES : NO;
    
}

#pragma mark - 创建视图

- (void)_createViewController {
    MovieViewController *movie = [[MovieViewController alloc] init];
    NewsViewController *news = [[NewsViewController alloc] init];
    TopViewController *top = [[TopViewController alloc] init];
    CinemaViewController *cinema = [[CinemaViewController alloc] init];
    MoreViewController *more = [[MoreViewController alloc] init];
    
    nvcArray = [[NSMutableArray alloc] init];
    NSArray *vcArray = @[movie, news, top, cinema, more];
    for (int i = 0; i < 5; i++) {
        BaseNavController *nvc = [[BaseNavController alloc] initWithRootViewController:vcArray[i]];
        
        [nvcArray addObject:nvc];
    }
    self.viewControllers = nvcArray;
}

- (void)_setTabBar {
    Class cls = NSClassFromString(@"UITabBarButton");
    for (UIView *view in self.tabBar.subviews) {
        if ([view isKindOfClass:cls]) {
            [view removeFromSuperview];
        }
    }
    NSArray *imageName = @[@"movie_cinema", @"msg_new", @"more_info", @"icon_cinema", @"more_select_setting"];
    NSArray *title = @[@"电影", @"新闻", @"top", @"影院", @"更多"];
    [self.tabBar setBackgroundImage:[UIImage imageNamed:@"tab_bg_all"]];
    
    CGFloat width = self.view.frame.size.width / 5;
    _selectedView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, width, 49)];
    _selectedView.image = [UIImage imageNamed:@"selectTabbar_bg_all1"];
    [self.tabBar addSubview:_selectedView];
    
    for (int i = 0;  i < 5; i++) {
        LKButton *button = [[LKButton alloc] initWithFrame:CGRectMake(i * width, 0, width, 49) withImageName:imageName[i] withLabelText:title[i]];
        button.tag = i;
        [button addTarget:self action:@selector(bAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.tabBar addSubview:button];
    }
    self.tabBar.translucent = YES;
}

- (void)bAction: (UIButton *)button {
    self.selectedIndex = button.tag;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:.3];
    _selectedView.center = button.center;
    [UIView commitAnimations];
}

- (void)viewDidLoad {
    [self _createViewController];
    [self _setTabBar];
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
